package com.sdkj.recruit.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.sdkj.recruit.dao.ICollectionResumeDao;
import com.sdkj.recruit.dao.ICompanyDao;
import com.sdkj.recruit.dao.ILabelDao;
import com.sdkj.recruit.dao.ILoginLogDao;
import com.sdkj.recruit.dao.IPostInformationDao;
import com.sdkj.recruit.dao.IReportInfoDao;
import com.sdkj.recruit.dao.IWeixinConfigDao;
import com.sdkj.recruit.model.CollectionResume;
import com.sdkj.recruit.model.Company;
import com.sdkj.recruit.model.CompanyEnvironment;
import com.sdkj.recruit.model.CompanyResume;
import com.sdkj.recruit.model.Education;
import com.sdkj.recruit.model.EducationalExperience;
import com.sdkj.recruit.model.Experience;
import com.sdkj.recruit.model.Label;
import com.sdkj.recruit.model.LoginLog;
import com.sdkj.recruit.model.Position;
import com.sdkj.recruit.model.PositionCollection;
import com.sdkj.recruit.model.PostInformation;
import com.sdkj.recruit.model.ReportInfo;
import com.sdkj.recruit.model.ResultCode;
import com.sdkj.recruit.model.ResultData;
import com.sdkj.recruit.model.Resume;
import com.sdkj.recruit.model.WeixinConfig;
import com.sdkj.recruit.model.WorkExperience;
import com.sdkj.recruit.mongoSupport.QueryParamCreator;
import com.sdkj.recruit.service.ICompanyService;
import com.sdkj.recruit.util.CusAccessObjectUtil;
import com.sdkj.recruit.util.DateUtil;
import com.sdkj.recruit.util.HttpAccessUtil;
import com.sdkj.recruit.util.LocationUtils;
import com.sdkj.recruit.util.PropertiesUtil;
import com.sdkj.recruit.util.RandomUtil;
import com.sdkj.recruit.util.RequestUtils;
import com.sdkj.recruit.util.StringUtil;
import com.sdkj.recruit.vo.CompanyEnvironmentVo;
import com.sdkj.recruit.vo.CompanyResumeVo;
import com.sdkj.recruit.vo.CompanyVo;
import com.sdkj.recruit.vo.EducationalExperienceVo;
import com.sdkj.recruit.vo.LabelVo;
import com.sdkj.recruit.vo.LoginLogVo;
import com.sdkj.recruit.vo.PositionCollectionVo;
import com.sdkj.recruit.vo.PositionVo;
import com.sdkj.recruit.vo.PostInformationVo;
import com.sdkj.recruit.vo.ReportInfoVo;
import com.sdkj.recruit.vo.ResumeVo;
import com.sdkj.recruit.vo.WeixinConfigVo;
import com.sdkj.recruit.vo.WorkExperienceVo;

@Service
public class CompanyService implements ICompanyService {

	@Autowired
	private ICompanyDao companyDao;

	@Autowired
	private ILoginLogDao logDao;
	
	@Autowired
	private IReportInfoDao reportDao;

	@Autowired
	private IPostInformationDao postInformationDao;
	
	@Autowired
	private Mapper mapper;
	
	@Autowired
	private ILabelDao labelDao;
	
	@Autowired
	private IWeixinConfigDao weixinConfigDao;
	
	@Autowired
	private ICollectionResumeDao collectionDao;
	
	
	private static final PropertiesUtil propertiesUtil = new PropertiesUtil("config.properties");

	@Override
	public ResultData<Object> saveCompany(CompanyVo companyVo) {

		if (!StringUtil.isEmptyString(companyVo.getMobile())) {

			Company company = mapper.map(companyVo, Company.class);
			company.setCompanyId(new RandomUtil().getRandomId());
			company.setCreateDate(new Date());
			companyDao.saveCompany(company);
			return ResultCode.success("保存成功", companyVo);
		}
		return ResultCode.error("保存失败", null);
	}

	@Override
	public ResultData<Object> queryCompanyById(String companyId) {

		Company company = companyDao.queryCompanyById(companyId);
		
		CompanyVo vo =mapper.map(company, CompanyVo.class);
		// 获取企业标签
		List<Label> list =labelDao.queryLableByCompanyId(companyId);
		
		// 获取办公环境
		List<CompanyEnvironment> ceList =companyDao.queryCompanyEnvironmentByCompanyId(companyId);
		
		if(!CollectionUtils.isEmpty(ceList)) {
			List<CompanyEnvironmentVo> volist = new ArrayList<CompanyEnvironmentVo>();
			for(CompanyEnvironment companyEnvironment :ceList) {
				CompanyEnvironmentVo vos =mapper.map(companyEnvironment, CompanyEnvironmentVo.class);
				volist.add(vos);
			}
			vo.setCompanyPics(volist);
		}
		
		if(!CollectionUtils.isEmpty(list)) {
			List<LabelVo> volist = new ArrayList<LabelVo>();
			for(Label l :list) {
				LabelVo vos =mapper.map(l, LabelVo.class);
				volist.add(vos);
			}
			vo.setLableList(volist);
		}
		return ResultCode.success("查询成功", vo);
	}

	@Override
	public ResultData<Object> deleteCompany(String companyId) {
		companyDao.deleteCompany(companyId);
		return ResultCode.success("删除成功", companyId);
	}

	@Override
	public List<CompanyVo> queryCompanyList(int pageNo, int pageSize, int companyType) {

		List<Company> list = companyDao.queryCompanyList(pageNo, pageSize, companyType);
		List<CompanyVo> volist = new ArrayList<CompanyVo>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (Company company : list) {
				CompanyVo vo = mapper.map(company, CompanyVo.class);

				// 获取企业的标签信息
				List<Label> lableList = companyDao.queryCompanyLableList(vo.getCompanyId());
				if (!CollectionUtils.isEmpty(lableList)) {
					List<LabelVo> voList = new ArrayList<LabelVo>();
					for (Label l : lableList) {
						LabelVo labelVo = mapper.map(l, LabelVo.class);
						voList.add(labelVo);
					}
					vo.setLableList(voList);
				}
				volist.add(vo);
			}
		}
		return volist;
	}

	@Override
	public long queryAllCompanyCount(int companyType) {
		return companyDao.queryAllCompanyCount(companyType);

	}

	@Override
	public ResultData<Object> report(ReportInfoVo reportVo) {

		if (StringUtil.isEmptyString(reportVo.getCompanyId())) {
			return ResultCode.error("信息不完整", reportVo);
		}
		ReportInfo report = mapper.map(reportVo, ReportInfo.class);
		reportDao.report(report);
		if (StringUtil.isEmptyString(report.getId())) {
			return ResultCode.error("举报失败", report);
		}
		return ResultCode.success("举报成功", report);
	}

	@Override
	public CompanyVo queryCompanyByMobile(String mobile) {

		Company c = companyDao.queryCompanyByMobile(mobile);
		if (c != null) {
			return mapper.map(c, CompanyVo.class);
		}
		return null;
	}

	@Override
	public void updatePassword(String mobile, String password) {
		companyDao.updatePassword(mobile, password);
	}

	@Override
	public void cleanUserLoginInfo(String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("accessToken", null);
		map.put("isOnline", false);
		map.put("expireTime", 0);
		companyDao.saveOrUpdateDocument(Company.class, map, QueryParamCreator.eq("companyId", userId));
	}

	@Override
	public List<ResumeVo> queryResumeList(int pageNo, int pageSize, String keyWord,String companyId,String workYearType) {

		List<Resume> list = companyDao.queryResumeList(pageNo, pageSize, workYearType);
		if (CollectionUtils.isNotEmpty(list)) {
			List<ResumeVo> voList = new ArrayList<ResumeVo>();
			for (Resume resume : list) {
				ResumeVo vo = mapper.map(resume, ResumeVo.class);
				
				// 判断是否被收藏
				CollectionResume cr= collectionDao.queryCollectionRecord(companyId, resume.getResumeId());
				if(cr != null) {
					vo.setCollectionStatus(1);
				}
				voList.add(vo);
			}
			return voList;
		}
		return null;
	}

	@Override
	public long queryAllResumeCount(String keyWord,String workYearType) {
		return companyDao.queryResumeList(workYearType);
	}

	@Override
	public List<CompanyResumeVo> queryDeliverResumeList(int pageNo, int pageSize, String keyWord, String companyId) {

		// 获取投递关系信息
		List<CompanyResume> list = companyDao.queryDeliverResumeList(pageNo, pageSize, keyWord, companyId);
		if (CollectionUtils.isNotEmpty(list)) {

			List<CompanyResumeVo> voList = new ArrayList<CompanyResumeVo>();
			for (CompanyResume dr : list) {
				CompanyResumeVo vo = mapper.map(dr, CompanyResumeVo.class);
				Resume r = companyDao.queryResumeById(vo.getResumeId());
				
				// 获取岗位信息
				Position p =companyDao.queryPositionById(dr.getPositionId());
				if(p !=null) {
					
					PostInformation pf =postInformationDao.queryPostById(p.getPositionType3());
					if(pf !=null) {
						vo.setPosition(pf.getPostInformationName());
					}
				}
				
				if (r != null) {
					vo.setSalaryBegin(r.getSalaryBegin());
					vo.setSalaryEnd(r.getSalaryEnd());
					vo.setPosition(vo.getPosition());
					vo.setWordYear(r.getWorkYear());
					vo.setUserName(r.getUserName());
					vo.setSex(r.getSex());
					// 判断是否被收藏
					CollectionResume cr =collectionDao.queryCollectionRecord(companyId, r.getResumeId());
					if(cr !=null) {
						vo.setCollectionStatus(1);
					}
				}
				voList.add(vo);
			}

			return voList;
		}
		return null;
	}

	@Override
	public long queryAllDeliverResumeCount(String keyWord, String companyId) {
		return companyDao.queryAllDeliverResumeCount(keyWord, companyId);
	}

	@Override
	public ResultData<Object> savePosition(PositionVo positionVo) {

		Position p = mapper.map(positionVo, Position.class);
		
		Company c =companyDao.queryCompanyById(positionVo.getCompanyId());
		if(c !=null) {
			p.setLatitude(c.getLatitude());
			p.setLongitude(c.getLongitude());
			// 设置location
			double[] a = new double[] { Double.valueOf(p.getLongitude()),
					Double.valueOf(p.getLatitude()) };
			p.setLocation(a);
		}
		p.setCreateDate(new Date());
		companyDao.savePosition(p);
		return ResultCode.success("保存成功", p);
	}

	@Override
	public List<PositionVo> queryPositionList(int pageNo, int pageSize, String keyWord, String companyId) {
		List<Position> list = companyDao.queryPositionList(pageNo, pageSize, keyWord, companyId);
		if (CollectionUtils.isNotEmpty(list)) {
			List<PositionVo> voList = new ArrayList<PositionVo>();
			for (Position p : list) {
				PositionVo vo = mapper.map(p, PositionVo.class);

				Company c = companyDao.queryCompanyById(vo.getCompanyId());
				if (c != null) {
					vo.setCompanyType(c.getCompanyType());
				}
				if (!StringUtil.isEmptyString(vo.getExperience())) {
					vo.setExperience(Experience.getName(Integer.valueOf(vo.getExperience())));
				}
				if (!StringUtil.isEmptyString(vo.getEducation())) {
					vo.setEducation(Education.getName(Integer.valueOf(vo.getEducation())));
				}
				voList.add(vo);
			}
			return voList;
		}
		return null;
	}

	@Override
	public long queryAllPositionCount(String keyWord, String companyId) {
		return companyDao.queryAllPositionCount(keyWord, companyId);
	}

	@Override
	public ResultData<Object> queryWorkExperienceById(String id) {
		WorkExperience work = companyDao.queryWorkExperienceById(id);
		return ResultCode.success("获取成功", work);
	}

	@Override
	public ResultData<Object> queryEducationalExperienceById(String id) {
		EducationalExperience ee = companyDao.queryEducationalExperienceById(id);
		return ResultCode.success("获取成功", ee);
	}

	@Override
	public ResultData<Object> initEditPosition(String id, String userId) {
		Position p = companyDao.queryPositionById(id);

		PositionVo vo = mapper.map(p, PositionVo.class);
		//PositionCollection pc = companyDao.queryCollectionById(id, userId);
		Company c = companyDao.queryCompanyById(vo.getCompanyId());
		if (c != null) {
			vo.setCompanyName(c.getCompanyName());
			vo.setCompanyLogo(c.getLogoUrl());
			vo.setAddress(c.getAddress());
			vo.setCompanyLogo(c.getLogoUrl());
			vo.setCompanyTelephone(c.getCompanyTelephone());
			vo.setMobile(c.getMobile());
		    String tx =map_bd2tx(c.getLatitude(), c.getLongitude());
			vo.setLatitude(tx.split(",")[0]);
			vo.setLongitude(tx.split(",")[1]);
			List<CompanyEnvironment> list =companyDao.queryCompanyEnvironmentByCompanyId(c.getCompanyId());

			if(!CollectionUtils.isEmpty(list)) {
				List<CompanyEnvironmentVo> voList =new ArrayList<CompanyEnvironmentVo>();
				for(CompanyEnvironment ce:list) {
					CompanyEnvironmentVo vo1 =mapper.map(ce, CompanyEnvironmentVo.class);
					voList.add(vo1);
				}
				vo.setCompanyPics(voList);
			}
			
			PositionCollection pc = companyDao.queryCollectionById(id, userId);
			if (pc != null) {
				vo.setCollectionStatus(1);
			}
			
			vo.setAddressPic(c.getAddressPic());
		}
		// 职位浏览数增加1
		companyDao.addCount(id);
		return ResultCode.success("获取成功", vo);
	}

	@Override
	public List<PositionVo> queryPositionListNew(int pageNo, int pageSize, String companyId, String userId) {
		List<Position> list = companyDao.queryPositionList(pageNo, pageSize, companyId);
		if (CollectionUtils.isNotEmpty(list)) {
			List<PositionVo> voList = new ArrayList<PositionVo>();
			for (Position p : list) {
				PositionVo vo = mapper.map(p, PositionVo.class);
				if (!StringUtil.isEmptyString(vo.getExperience())) {
					vo.setExperience(Experience.getName(Integer.valueOf(vo.getExperience())));
				}
				if (!StringUtil.isEmptyString(vo.getEducation())) {
					vo.setEducation(Education.getName(Integer.valueOf(vo.getEducation())));
				}
				Company c = companyDao.queryCompanyById(companyId);
				if (c != null) {
					vo.setAddress(c.getAddress());
					vo.setCompanyTelephone(c.getMobile());
					vo.setCompanyName(c.getCompanyName());
					vo.setCompanyLogo(c.getLogoUrl());
				}
				PositionCollection pc = companyDao.queryCollectionById(vo.getId(), userId);
				if (pc != null) {
					vo.setCollectionStatus(1);
				}
				voList.add(vo);
			}
			return voList;
		}
		return null;
	}

	@Override
	public long queryAllPositionCount(String companyId) {
		return companyDao.queryAllPositionCount(companyId);
	}

	@Override
	public ResultData<Object> updatePosition(PositionVo positionVo) {
		Position p = mapper.map(positionVo, Position.class);
		companyDao.updatePosition(p);
		return ResultCode.success("更新成功", positionVo);
	}

	@Override
	public ResultData<Object> collection(String userId, String positionId, int collectionType) {

		Position po = companyDao.queryPositionById(positionId);
		PositionCollection p = new PositionCollection();
		p.setCollectionDate(new Date());
		p.setPositionId(positionId);
		p.setUserId(userId);
		if (po != null) {
			p.setCompanyId(po.getCompanyId());
		}
		if (collectionType == 1) {
			companyDao.collection(p);
			return ResultCode.success("收藏成功", p);
		} else {
			companyDao.deleteCollection(userId, positionId);
			return ResultCode.success("取消收藏成功", p);
		}
	}

	@Override
	public List<PositionCollectionVo> querycollectionList(int pageNo, int pageSize, String userId) {
		List<PositionCollection> list = companyDao.querycollectionList(pageNo, pageSize, userId);
		if (CollectionUtils.isNotEmpty(list)) {
			List<PositionCollectionVo> voList = new ArrayList<PositionCollectionVo>();
			for (PositionCollection p : list) {
				PositionCollectionVo vo = mapper.map(p, PositionCollectionVo.class);

				Company c = companyDao.queryCompanyById(p.getCompanyId());
				if (c == null) {
					continue;
				}

				vo.setAddress(c.getAddress());
				vo.setCompanyTelephone(c.getCompanyTelephone());
				vo.setCompanyName(c.getCompanyName());
				vo.setCompanyLogo(c.getLogoUrl());
				Position pc = companyDao.queryPositionById(vo.getPositionId());
				if (p != null) {
					vo.setCompanyName(c.getCompanyName());
				}

				if (pc != null) {

					if (!StringUtil.isEmptyString(pc.getExperience())) {
						vo.setExperience(Experience.getName(Integer.valueOf(pc.getExperience())));
					}
					if (!StringUtil.isEmptyString(pc.getEducation())) {
						vo.setEducation(Education.getName(Integer.valueOf(pc.getEducation())));
					}
					vo.setSalaryBegin(pc.getSalaryBegin());
					vo.setSalaryEnd(pc.getSalaryEnd());
					vo.setWorkNature(pc.getWorkNature());
					vo.setPositionName(pc.getPositionName());
				}
				voList.add(vo);
			}
			return voList;
		}
		return null;
	}

	@Override
	public long queryAllCompanyCount(String userId) {
		return 0;
	}

	@Override
	public long queryAllCollectionCount(String userId) {
		return companyDao.queryAllCollectionCount(userId);
	}

	@Override
	public ResultData<Object> updateStatus(String id, int status) {

		companyDao.updateStatus(id, status);
		return ResultCode.success("修改成功", null);
	}

	@Override
	public ResultData<Object> deliverList(int pageNo, int pageSize, String userId) {

		List<CompanyResumeVo> voList = new ArrayList<CompanyResumeVo>();
		List<CompanyResume> list = companyDao.deliverList(pageNo, pageSize, userId);
		if (CollectionUtils.isNotEmpty(list)) {
			for (CompanyResume p : list) {
				CompanyResumeVo vo = mapper.map(p, CompanyResumeVo.class);

				Company c = companyDao.queryCompanyById(p.getCompanyId());

				if (c == null) {
					continue;
				}
				Position pc = companyDao.queryPositionById(vo.getPositionId());
				if (p != null) {
					vo.setPositionName(pc.getPositionName());
					vo.setCompanyName(c.getCompanyName());
				}
				vo.setCompanyName(c.getCompanyName());
				vo.setCompanyLogo(c.getLogoUrl());
				vo.setAddress(c.getAddress());
				vo.setCompanyTelephone(c.getCompanyTelephone());
				vo.setMobile(c.getMobile());
				if (pc != null) {

					if (!StringUtil.isEmptyString(pc.getExperience())) {
						vo.setExperience(Experience.getName(Integer.valueOf(pc.getExperience())));
					}
					if (!StringUtil.isEmptyString(pc.getEducation())) {
						vo.setEducation(Education.getName(Integer.valueOf(pc.getEducation())));
					}
					vo.setSalaryBegin(pc.getSalaryBegin());
					vo.setSalaryEnd(pc.getSalaryEnd());
					vo.setWorkNature(pc.getWorkNature());
				}
				voList.add(vo);
			}
		}
		return ResultCode.success("获取结果成功", voList);
	}

	@Override
	public ResultData<Object> auth(CompanyVo companyVo) {
		
		String companyId =companyVo.getCompanyId();
		
		Map<String,Object> maps =new HashMap<String,Object>();
		if(StringUtil.isNotEmptyString(companyVo.getCompanyName())) {
			maps.put("companyName", companyVo.getCompanyName());
		}
		
		if(StringUtil.isNotEmptyString(companyVo.getLogoUrl())) {
			maps.put("logoUrl", companyVo.getLogoUrl());
		}
		
		if(StringUtil.isNotEmptyString(companyVo.getCompanyDesc())) {
			maps.put("companyDesc", companyVo.getCompanyDesc());
		}
		
		if(StringUtil.isNotEmptyString(companyVo.getAddress())) {
			maps.put("address", companyVo.getAddress());
		}
		
		if(StringUtil.isNotEmptyString(companyVo.getCompanyTelephone())) {
			maps.put("companyTelephone", companyVo.getCompanyTelephone());
		}
		
		if(companyVo.getLatitude()!=0 && companyVo.getLongitude()!=0) {
			maps.put("latitude", companyVo.getLatitude());
			maps.put("longitude", companyVo.getLongitude());
			
			// 设置location
			double[] a = new double[] { Double.valueOf(companyVo.getLongitude()),
					Double.valueOf(companyVo.getLatitude()) };
			
			maps.put("location", a);
		}
		
		if(StringUtil.isNotEmptyString(companyVo.getYyzzUrl())) {
			maps.put("yyzzUrl", companyVo.getYyzzUrl());
		}
		
		// 身份证正面
		if(StringUtil.isNotEmptyString(companyVo.getIdentificationCardfront())) {
			maps.put("identificationCardfront", companyVo.getIdentificationCardfront());
		}
		// 身份证反面
		if(StringUtil.isNotEmptyString(companyVo.getIdentificationCardback())) {
			maps.put("identificationCardback", companyVo.getIdentificationCardback());
		}
		maps.put("compamnyAuthentication", 1);
		
		// 更新企业信息
		companyDao.updateCompany(maps,companyId);
		
		// 更新企业标签（删除-新增）
		if(StringUtil.isNotEmptyString(companyVo.getLabelStringList())) {
			String [] lables =companyVo.getLabelStringList().split(",");
			
			List<Label> list =new ArrayList<Label>();
			for(String name:lables) {
				Label lable =new Label();
				lable.setCompanyId(companyId);
				lable.setLabelName(name.split("@")[0]);
				lable.setCreateDate(new Date());
				list.add(lable);
			}
			labelDao.deleteLabelByComapanyId(companyId);
			labelDao.saveLabelList(list);
		}
		return ResultCode.success("认证成功", companyVo);
	}

	@Override
	public ResultData<Object> queryPositions(int pageNo, int pageSize, int queryType,String userId,String longitude, String latitude,String keyWord) {
		
		String location =map_tx2bd(Double.valueOf(latitude), Double.valueOf(longitude));
		String[] x =location.split(",");
		
		List<Position> list = companyDao.queryPositionList(pageNo, pageSize, queryType,keyWord);
		if (CollectionUtils.isNotEmpty(list)) {
			List<PositionVo> voList = new ArrayList<PositionVo>();
			for (Position p : list) {
				PositionVo vo = mapper.map(p, PositionVo.class);
				if (!StringUtil.isEmptyString(vo.getExperience())) {
					vo.setExperience(Experience.getName(Integer.valueOf(vo.getExperience())));
				}
				if (!StringUtil.isEmptyString(vo.getEducation())) {
					vo.setEducation(Education.getName(Integer.valueOf(vo.getEducation())));
				}
				Company c = companyDao.queryCompanyById(vo.getCompanyId());
				if (c != null) {
					vo.setDistance((new Double(LocationUtils.getDistance(Double.valueOf(x[1]), Double.valueOf(x[0]), c.getLatitude(), c.getLongitude()))).intValue());

					vo.setAddress(c.getAddress());
					vo.setCompanyTelephone(c.getCompanyTelephone());
					vo.setMobile(c.getMobile());
					vo.setCompanyName(c.getCompanyName());
					vo.setCompanyLogo(c.getLogoUrl());
				}
				PositionCollection pc = companyDao.queryCollectionById(vo.getId(), userId);
				if (pc != null) {
					vo.setCollectionStatus(1);
				}
				voList.add(vo);
			}
			return ResultCode.success("查询成功", voList);
		}
		return ResultCode.success("查询成功", null);
	}

	@Override
	public void registerLog(HttpServletRequest request,String companyId) {

		LoginLog log =new LoginLog();
		log.setLoginDate(new Date());
		String ip =CusAccessObjectUtil.getIpAddress(request);
		log.setLoginIp(ip);
		String address= CusAccessObjectUtil.baiduGetCityCode(ip);
		log.setAddress(address);
		int clientType=RequestUtils.getClientFlag(request);
		log.setClientType(String.valueOf(clientType));
		log.setCompanyId(companyId);
		logDao.registerLog(log);
	}

	@Override
	public long queryAllLogsCount(String companyId,int queryType) {
		// 查询当天
		if(queryType==1) {
			Date begin =DateUtil.strToDate(DateUtil.dataToString(new Date(), "yyyy-MM-dd")+" 00:00:00", "yyyy-MM-dd HH:mm:ss");
			Date end =DateUtil.strToDate(DateUtil.dataToString(new Date(), "yyyy-MM-dd")+" 23:59:59", "yyyy-MM-dd HH:mm:ss");
			return logDao.queryAllLogsCount(companyId,begin,end);
		}
		if(queryType==2) {
			Date end =DateUtil.strToDate(DateUtil.theDayBefore()+" 23:59:59", "yyyy-MM-dd HH:mm:ss");
			return logDao.queryAllLogsCount(companyId,end);
		}
		return 0;
	}

	@Override
	public List<LoginLogVo> queryLoginLogs(int pageNo, int pageSize, String companyId,int queryType) {
		
		List<LoginLogVo> volist =new ArrayList<LoginLogVo>();
		List<LoginLog> list =new ArrayList<LoginLog>();
		// 查询当天
		if (queryType == 1) {
			Date begin = DateUtil.strToDate(DateUtil.dataToString(new Date(), "yyyy-MM-dd") + " 00:00:00",
					"yyyy-MM-dd HH:mm:ss");
			Date end = DateUtil.strToDate(DateUtil.dataToString(new Date(), "yyyy-MM-dd") + " 23:59:59",
					"yyyy-MM-dd HH:mm:ss");
			list = logDao.queryLoginLogs( pageNo,  pageSize,companyId, begin, end);
		}
		if (queryType == 2) {
			Date end = DateUtil.strToDate(DateUtil.theDayBefore() + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
			list =  logDao.queryLoginLogs(pageNo, pageSize,companyId, end);
		}
		
		if(!CollectionUtils.isEmpty(list)) {
			for(LoginLog log :list) {
				LoginLogVo vo =mapper.map(log, LoginLogVo.class);
				volist.add(vo);
			}
		}
		return volist;
	}

	@Override
	public ResumeVo queryResumeById(String resumeId) {
		
		ResumeVo vo =new ResumeVo();
		Resume re =companyDao.queryResumeById(resumeId);
		vo =mapper.map(re, ResumeVo.class);
		
		// 获取工作经验
		List<WorkExperience> workList =companyDao.queryWorkExperienceByResumeId(resumeId);
		
		// 获取教育经历
		List<EducationalExperience> eduList =companyDao.queryEducationalExperienceByResumeId(resumeId);

		if(CollectionUtils.isNotEmpty(workList)) {
			
			List<WorkExperienceVo> workVoList =new ArrayList<WorkExperienceVo>();
			for(WorkExperience work :workList) {
				WorkExperienceVo vow =mapper.map(work, WorkExperienceVo.class);
				workVoList.add(vow);
			}
			vo.setWorkList(workVoList);
		}
		
		if(CollectionUtils.isNotEmpty(eduList)) {
			
			List<EducationalExperienceVo> eduVoList =new ArrayList<EducationalExperienceVo>();
			for(EducationalExperience work :eduList) {
				EducationalExperienceVo vow =mapper.map(work, EducationalExperienceVo.class);
				eduVoList.add(vow);
			}
			vo.setEduList(eduVoList);
		}
		return vo;
	}

	@Override
	public ResultData<Object> saveCompanyPic(CompanyVo companyVo) {
		String companyId =companyVo.getCompanyId();
		
		String companyPics =companyVo.getCompanyUrls();
		
		if(StringUtil.isNotEmptyString(companyPics)) {
			String [] pics =companyPics.split(",");
			Date currentDate =new Date();
			List<CompanyEnvironment> list =new ArrayList<CompanyEnvironment>();
			for(String picId:pics) {
				CompanyEnvironment ce =new CompanyEnvironment();
				ce.setCompanyId(companyId);
				ce.setPicId(picId);
				ce.setCreateDate(currentDate);
				list.add(ce);
			}
			companyDao.saveCompanyPic(list);
		}
		return ResultCode.success("保存成功", companyVo);
	}

	
	/**
	 * 坐标转换，腾讯地图转换成百度地图坐标
	 * @param lat 腾讯纬度
	 * @param lon 腾讯经度
	 * @return 返回结果：经度,纬度
	 */
	private String map_tx2bd(double lat, double lon){
		double bd_lat;
		double bd_lon;
		double x_pi=3.14159265358979324;
	    double x = lon, y = lat;
	    double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
	    double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
	    bd_lon = z * Math.cos(theta) + 0.0065;
	    bd_lat = z * Math.sin(theta) + 0.006;
	    
	    System.out.println("bd_lat:"+bd_lat);
	    System.out.println("bd_lon:"+bd_lon);
	    return bd_lon+","+bd_lat;
	}
	
	@Override
	public ResultData<Object> queryNearbyCompany(int pageNo, int pageSize, String longitude, String latitude,
			int distance) {
		
		String location =map_tx2bd(Double.valueOf(latitude), Double.valueOf(longitude));
		String[] x =location.split(",");
		List<Company> companyList =companyDao.queryNearbyCompany(pageNo,pageSize,x[0],x[1],distance);
		
		List<CompanyVo> volist = new ArrayList<CompanyVo>();
		if (CollectionUtils.isNotEmpty(companyList)) {
			for (Company company : companyList) {
				CompanyVo vo = mapper.map(company, CompanyVo.class);

				/*// 获取企业的标签信息
				List<Label> lableList = companyDao.queryCompanyLableList(vo.getCompanyId());
				if (!CollectionUtils.isEmpty(lableList)) {
					List<LabelVo> voList = new ArrayList<LabelVo>();
					for (Label l : lableList) {
						LabelVo labelVo = mapper.map(l, LabelVo.class);
						voList.add(labelVo);
					}
					vo.setLableList(voList);
				}*/
				volist.add(vo);
			}
		}
		return ResultCode.success("查询成功", volist);
	}

	/**
	 * 坐标转换，百度地图坐标转换成腾讯地图坐标
	 * @param lat  百度坐标纬度
	 * @param lon  百度坐标经度
	 * @return 返回结果：纬度,经度
	 */
	public String map_bd2tx(double lat, double lon){
		double tx_lat;
		double tx_lon;
		double x_pi=3.14159265358979324;
		double x = lon - 0.0065, y = lat - 0.006;
	    double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
	    double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
	    tx_lon = z * Math.cos(theta);
	    tx_lat = z * Math.sin(theta);
	    return tx_lat+","+tx_lon;
	}
	
	@Override
	public ResultData<Object> jscode2session(String code) {
		String appid =propertiesUtil.getProperty("weixinappId");
		String appkey =propertiesUtil.getProperty("weixinappKey");
		String grant_type ="authorization_code";
		
		String test =HttpAccessUtil.httpGetSSL("https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+appkey+"&js_code="+code+"&grant_type="+grant_type);
		
		// 存储信息
		String userunionid =new RandomUtil().getRandomId();
		WeixinConfig  wc =new WeixinConfig();
		
		JSONObject josn =JSONObject.parseObject(test);
		
		if(josn !=null && !josn.isEmpty()) {
			
			Object session_key =josn.get("session_key");
			Object openid =josn.get("openid");
			Object unionid =josn.get("unionid");

			wc.setOpenid(StringUtil.valueOf(openid));
			wc.setSession_key(StringUtil.valueOf(session_key));
			wc.setUniqueId(StringUtil.valueOf(unionid));
			wc.setUniqueId(userunionid);
			weixinConfigDao.saveWeixinConfig(wc);
			return ResultCode.success("获取认证信息成功", userunionid);
		}
		return ResultCode.success("获取认证信息失败", null);
		
	}

	@Override
	public WeixinConfigVo querySessionKeyByUniqueId(String uniqueId) {
		
		WeixinConfig wc =weixinConfigDao.querySessionKeyByUniqueId(uniqueId);
		
		if(wc !=null) {
			return mapper.map(wc, WeixinConfigVo.class);
		}
		return null;
	}

	@Override
	public void save(PostInformation pi1) {
		postInformationDao.save(pi1);
	}

	@Override
	public ResultData<Object> queryPostIno(String parentId, int queryType) {
		List<PostInformation> list =postInformationDao.queryPostIno(parentId,queryType);
		
		if(!CollectionUtils.isEmpty(list)) {
			List<PostInformationVo> volist =new ArrayList<PostInformationVo>();
			for(PostInformation p :list) {
				PostInformationVo vo =mapper.map(p, PostInformationVo.class);
				volist.add(vo);
			}
			return ResultCode.success("获取成功", volist);
		}
		return ResultCode.error("获取失败", null);
	}

	@Override
	public PostInformation queryPostByName(String f1) {
		return postInformationDao.queryPostByName(f1);
	}

	@Override
	public ResultData<Object> collectionResume(String companyId, List<String> resumeIds) {
		
		// 验证是否已经收藏，已经收藏，跳过
		
		if(!CollectionUtils.isEmpty(resumeIds)) {
			for(String resumeId :resumeIds) {
				CollectionResume cr =collectionDao.queryCollectionRecord(companyId,resumeId);
				if(cr == null) {
					CollectionResume cre =new CollectionResume();
					cre.setCollectionDate(new Date());
					cre.setCompanyId(companyId);
					cre.setResumeId(resumeId);
					collectionDao.saveCollectionResume(cre);
				}
			}
		}
		return ResultCode.success("收藏成功", null);
	}

	@Override
	public ResultData<Object> check(String mobile, String checkcode, int type) {
		return null;
	}

	@Override
	public void updateCompanyMobile(String mobile, String companyId) {
		Map<String, Object> obj =new HashMap<String, Object>();
		obj.put("mobile", mobile);
		companyDao.updateCompanyMobile(companyId,obj);
	}

	@Override
	public PostInformation queryPostByName(String name, String pid) {
		return postInformationDao.queryPostByName(name,pid);
	}

	@Override
	public void updateLaction() {
		
		companyDao.queryAllPositions();
	}

	@Override
	public void sethotCompany() {
		companyDao.sethotCompany();
	}

	@Override
	public void updateNewPassword(String mobile, String newpsw) {
		companyDao.updateNewPassword(mobile, newpsw);
	}
}
